﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace SCCN.Utility
{
    public class XMLHelper
    {
        /// <summary>
        /// 根据xml字符串产生XmlDocument对象
        /// </summary>
        /// <param name="strxml">xml字符串</param>
        /// <returns>XmlDocument</returns>
        public static XmlDocument GetXmlDocument(String strXml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(strXml);
            return doc;
        }

        /// <summary>
        /// 根据xml字符串产生XDocument对象
        /// </summary>
        /// <param name="strxml">xml字符串</param>
        /// <returns>XDocument</returns>
        public static XDocument GetXDocument(String strXml)
        {
            return XDocument.Parse(strXml); 
        }

        /// <summary>
        /// 根据元素名称取出单节点元素的值
        /// </summary>
        /// <param name="elementname">元素名称</param>
        /// <param name="strxml">源xml文件</param>
        /// <returns>元素值</returns>
        public static String GetSingleElementValue(string elementName, String strXml)
        {
            string elementValue = string.Empty;
            XDocument doc = GetXDocument(strXml);
            var item = from s in doc.Descendants(elementName)
                       select s;
            foreach (XElement xe in item)
            {
                elementValue = xe.Value.ToString();
            }
            return elementValue;
        }

        /// <summary>
        /// 得到xml数据集合
        /// </summary>
        /// <param name="strxml">xml文本</param>
        /// <returns>数据集合</returns>
        public static DataSet GetXmlToDataSet(String strXml)
        {
            //数据表集合
            DataSet xmlDataSet = new DataSet();
            //独立的数据表
            DataTable dt = null;
            //行节点集合
            XmlNodeList row = null;
            //单元格集合
            XmlNodeList cell = null;
            //数据行
            DataRow dr = null;
            XmlDocument xmldoc = GetXmlDocument(strXml);
            //得到根节点
            XmlElement root = xmldoc.DocumentElement;
            //得到表格数据集节点集合
            XmlNodeList tableNode = root.ChildNodes[0].ChildNodes;

            //遍历数据集节点
            foreach (XmlNode xn1 in tableNode)
            {
                dt = new DataTable(xn1.Attributes["MethodName"].Value);
                XmlNodeList header = xn1.FirstChild.ChildNodes;
                //创建数据列头信息
                foreach (XmlNode hd in header)
                {
                    dt.Columns.Add(hd.InnerXml);
                }
                //遍历数据行集合
                row = xn1.ChildNodes;
                foreach (XmlNode vrow in row)
                {
                    dr = dt.NewRow();
                    //排除列头信息
                    if (vrow.Name != "Header")
                    {
                        //遍历单元格集合
                        cell = vrow.ChildNodes;
                        for (int i = 0; i < vrow.ChildNodes.Count; i++)
                        {
                            dr[i] = vrow.ChildNodes[i].InnerText;
                        }
                        dt.Rows.Add(dr);
                    }
                }
                xmlDataSet.Tables.Add(dt);
            }
            return xmlDataSet; 
        } 

        /// <summary>
        /// 得到xml指定元素的指定属性值
        /// </summary>
        /// <param name="strxml">xml文本</param>
        /// <param name="elementname">元素节点名称</param>
        /// <param name="attributename">属性名称</param>
        /// <returns>属性值</returns>
        public static String GetElementsAttributesValue(String strXml, String elementName, String attributeName)
        {
            XDocument doc = GetXDocument(strXml);
            var protocol = from p in doc.Descendants(elementName)
                           select p.Attribute(attributeName).Value;
            string result = string.Empty;
            foreach (string var in protocol)
            {
                result = var;
            }
            return result.ToString();
        }
    }
}
